package com.ruoyi.civil.controller;

import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.civil.constant.MessageConstant;
import com.ruoyi.civil.domain.CivilEnginneeringRegularCheck;
import com.ruoyi.civil.entity.Result;
import com.ruoyi.civil.utils.QiniuUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.civil.service.ICivilEnginneeringRegularCheckService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 土建结构定期检查Controller
 *
 * @author 小赵
 * @date 2022-04-25
 */
@RestController
@RequestMapping("/civil/regularcheck")
public class CivilEnginneeringRegularCheckController extends BaseController
{
    @Autowired
    private ICivilEnginneeringRegularCheckService civilEnginneeringRegularCheckService;

    /**
     * 查询土建结构定期检查列表
     */
    @PreAuthorize("@ss.hasPermi('civil:regularcheck:list')")
    @GetMapping("/list")
    public TableDataInfo list(CivilEnginneeringRegularCheck civilEnginneeringRegularCheck)
    {
        startPage();
        List<CivilEnginneeringRegularCheck> list = civilEnginneeringRegularCheckService.selectCivilEnginneeringRegularCheckList(civilEnginneeringRegularCheck);
        return getDataTable(list);
    }

    /**
     * 导出土建结构定期检查列表
     */
    @PreAuthorize("@ss.hasPermi('civil:regularcheck:export')")
    @Log(title = "土建结构定期检查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CivilEnginneeringRegularCheck civilEnginneeringRegularCheck)
    {
        List<CivilEnginneeringRegularCheck> list = civilEnginneeringRegularCheckService.selectCivilEnginneeringRegularCheckList(civilEnginneeringRegularCheck);
        ExcelUtil<CivilEnginneeringRegularCheck> util = new ExcelUtil<CivilEnginneeringRegularCheck>(CivilEnginneeringRegularCheck.class);
        util.exportExcel(response, list, "土建结构定期检查数据");
    }

    /**
     * 获取土建结构定期检查详细信息
     */
    @PreAuthorize("@ss.hasPermi('civil:regularcheck:query')")
    @GetMapping(value = "/{regularCheckId}")
    public AjaxResult getInfo(@PathVariable("regularCheckId") Long regularCheckId)
    {
        return AjaxResult.success(civilEnginneeringRegularCheckService.selectCivilEnginneeringRegularCheckByRegularCheckId(regularCheckId));
    }

    /**
     * 新增土建结构定期检查
     */
    @PreAuthorize("@ss.hasPermi('civil:regularcheck:add')")
    @Log(title = "土建结构定期检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CivilEnginneeringRegularCheck civilEnginneeringRegularCheck) {
       int result= civilEnginneeringRegularCheckService.insertCivilEnginneeringRegularCheck(civilEnginneeringRegularCheck);
        System.out.println("+++++++++++++++++++++++++++++++++++"+result);
       if(result==0){
           return new AjaxResult().error("当前公路不存在，请重新输入");
       }else {
           return toAjax(civilEnginneeringRegularCheckService.insertCivilEnginneeringRegularCheck(civilEnginneeringRegularCheck));
       }
    }

    /**
     * 修改土建结构定期检查
     */
    @PreAuthorize("@ss.hasPermi('civil:regularcheck:edit')")
    @Log(title = "土建结构定期检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CivilEnginneeringRegularCheck civilEnginneeringRegularCheck)
    {
        return toAjax(civilEnginneeringRegularCheckService.updateCivilEnginneeringRegularCheck(civilEnginneeringRegularCheck));
    }

    /**
     * 删除土建结构定期检查
     */
    @PreAuthorize("@ss.hasPermi('civil:regularcheck:remove')")
    @Log(title = "土建结构定期检查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{regularCheckIds}")
    public AjaxResult remove(@PathVariable Long[] regularCheckIds)
    {
        return toAjax(civilEnginneeringRegularCheckService.deleteCivilEnginneeringRegularCheckByRegularCheckIds(regularCheckIds));
    }
    //图片上传
   @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try{
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(lastIndexOf - 1);
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //图片上传成功
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
            result.setData(fileName);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            //图片上传失败
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
}
